package top.zz;

public class QueueTest {

	public static void main(String[] args) {
		Queue myQueue = new MyQueue(10);
		myQueue.add(Integer.valueOf(1));
		myQueue.add(Integer.valueOf(2));
		myQueue.display();
		myQueue.add(Integer.valueOf(3));
		myQueue.add(Integer.valueOf(4));
		myQueue.display();

		int length = myQueue.size();

		for (int i = 0; i < length; i++) {
			System.out.println(myQueue.remove());
			myQueue.display();
		}
	}
}

interface Queue {

	void add(Object value);

	Object peekFront();

	void display();

	int size();

	Object remove();

}

class MyQueue implements Queue {

	private Object[] arrays;

	private int maxSize;

	/**
	 * 队列头索引
	 */
	private int headIndex = -1;

	/**
	 * 队列尾索引
	 */
	private int endIndex = -1;

	public MyQueue(int size) {
		arrays = new Object[size];
	}

	@Override
	public void add(Object value) {

		if (isFull()) {
			System.out.println("队列已满");
		} else {
			if (maxSize == 0) {
				headIndex++;
			}

			maxSize++;
			endIndex++;
			arrays[endIndex] = value;
		}
	}

	@Override
	public Object remove() {
		Object temp = null;
		if (maxSize == 0) {
			System.out.println("队列为空");
		} else if (maxSize == 1) {
			temp = arrays[headIndex];
			arrays[headIndex] = null;
			headIndex = -1;
			endIndex = -1;
			maxSize--;
		} else {
			temp = arrays[headIndex];
			arrays[headIndex] = null;
			headIndex++;
			maxSize--;
		}

		return temp;
	}

	@Override
	public Object peekFront() {

		return headIndex != -1 ? arrays[headIndex] : null;
	}

	public boolean isFull() {
		return maxSize == arrays.length;
	}

	@Override
	public void display() {

		System.out.print("=队列头= ");

		for (Object object : arrays) {
			if (object == null) {
				continue;
			}
			System.out.print(object + " ");
		}
		System.out.print(" =队列尾=");
		System.out.println();
	}

	@Override
	public int size() {
		return maxSize;
	}
}

class LinkedQueue implements Queue {
	DoublePointLinked doublePointLinked;

	public LinkedQueue() {
		doublePointLinked = new DoublePointLinked();
	}

	@Override
	public void add(Object value) {
		doublePointLinked.addTail(value);
		
	}

	@Override
	public Object peekFront() {
		
		return doublePointLinked.getHead();
	}

	@Override
	public void display() {
		doublePointLinked.display();
		
	}

	@Override
	public int size() {
		
		return doublePointLinked.size;
	}

	@Override
	public Object remove() {
		
		return doublePointLinked.removeHead();
	}

}